
const app = getApp()
const request = require('../../../utils/request');

Page({
	data: {
        card:[], //充值卡
        code: '',
        shareCode: wx.getStorageSync('code')
    },
    onLoad: function(){
        request.HttpRequst(true, 'getShareCode', false, '', {'token':wx.getStorageSync('sessionId')}, "post", false, res => {
            console.log(res);
            if(res.code == 200){
                this.setData({
                    code: res.data.code
                })
            }
        })
        request.HttpRequst(true, 'getShareCards', false, '', {'token':wx.getStorageSync('sessionId')}, "post", false, res => {
            console.log(res)
            this.setData({
                card: res.data
            })
        })
        wx.showShareMenu({
            withShareTicket: true
        })
    },
	onReady: function(){
        wx.setNavigationBarTitle({
            title: '优惠卡'
        })
    },
    recharge(e) {
        let money = e.currentTarget.dataset.money;
        request.HttpRequst(true, 'shareCard', false, '', {'token':wx.getStorageSync('sessionId'),'money':money,'code':this.data.shareCode}, "post", false, res => {
            console.log(res)
            if(res.code == 200){
                //调起支付接口
                request.HttpRequst(true,'pay',2,'',{"pay_way":'wechat', 'order_id':res.data.order_id, 'pay_port':'miniapp','payword':wx.getStorageSync("sessionId")},"POST",false,res => {
                    console.log(res);
                    if( res.code == '200') {
                        wx.requestPayment({
                            'timeStamp': res.data.timeStamp,
                            'nonceStr': res.data.nonceStr,
                            'package': res.data.package,
                            'signType': 'MD5',
                            'paySign': res.data.paySign,
                            'success':function(res){
                                console.log("支付成功");
                                wx.showModal({
                                    title: '提示',
                                    content: '支付成功',
                                    showCancel: false
                                })
                            },
                            'fail':function(res){
                                console.log("支付失败")
                            }
                        })
                    } 
                })
            }
        })
    },

    //分享
    onShareAppMessage: function (res) {
        return {
            title: '【掌里通】送你一张优惠卡，充200元得260元',
            path: '/pages/index/index?code='+this.data.shareCode,
            success: function(res) {
                console.log(res)
                // 转发成功
                wx.showToast({
                    title: '分享成功',
                    duration: 3000
                })

            },
            fail: function(res) {
                wx.showModal({
                    title: '提示',
                    content: '分享失败',
                    showCancel: false
                })
                // 分享失败
            }
        }
    }
})